package com.sxy.controller.Interceptor;

import com.alibaba.fastjson.JSON;
import com.sxy.controller.common.BaseContext;
import com.sxy.controller.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
@Slf4j
@Component
public class BSInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("拦截到请求：{}", request.getRequestURI());
        String uri = request.getRequestURI();

        Object employee = request.getSession().getAttribute("employee");
        System.out.println(employee);

        Object user = request.getSession().getAttribute("user");
        System.out.println(user);

        if (user != null) {
            //获取当前用户Id
            Long userId = (Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);
            return true;
        }

        if (employee != null){
            //获取当前用户Id
            Long empId = (Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empId);

            return true;
        }

        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return false;
    }
}
